<?php

/**
 * 权限控制器
 * @author yanghao <yh38615890@sina.cn>
 * @date 07-04-12
 * @copyright (c) gandianli
 */

namespace app\attendance\controller;

use app\attendance\Base;
use com\gdl\kq\dataobject\bulletin\bulletinDTO;
use com\gdl\kq\interfaces\AttAdminBulletinServiceClient;
use think\Config;
use think\Db;
use think\Exception;
use think\Loader;
use think\exception\HttpException;

class AttAdminBulletin extends Base
{

	public function index(){
	}
	//公告列表
	public function getBulletinList(){

		$filename['adminReg'] = $this->request->param('adminReg') ? getStr($this->request->param('adminReg'), 'string') : '';
		$page = $this->request->param('p')>1 ? $this->request->param('p/d') : 1;
		$pagesize = $this->request->param('num')>0 ? $this->request->param('num/d') : 10;
		$title = $this->request->param('title') ? getStr($this->request->param('title'), 'string') : '';
		$status = $this->request->param('status') ? getStr($this->request->param('status'), 'string') : '';//开启starts 禁用disabled
		$platformId = $this->request->param('platformId') ?$this->request->param('platformId/d') : 0;
		$id = $this->request->param('id') ?$this->request->param('id/d') : 0;

		$filename['platform'] = $this->request->param('platform') ? getStr($this->request->param('platform'), 'string') : '';
		if(empty($filename['adminReg']) || empty($platformId)){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}
		
		$filename['title'] = $title;
		$filename['status'] = $status;
		$filename['platformId'] = $platformId;
		$filename['page'] = $page;
		$filename['pagesize'] = $pagesize;
		
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new AttAdminBulletinServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new bulletinDTO();
				$engprojectDo->filename = $filename;
				$engprojectDo->id = $id;

				$result = $api->getBulletinList($engprojectDo);

				if($result->code){
					$data = ['msg'=>'成功','code'=>'200','data'=>$result->data,'count'=>$result->counts?$result->counts:0];
				} else {
					$data = ['msg'=>'失败','code'=>'400','data'=>[],'count'=>0];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'失败','code'=>'400','data'=>[],'count'=>0];
			}

			return json_encode($data);

	}

	//添加公告
	public function addBulletin(){
				$datas = $this->request->post();
				if (empty($datas['content']) || empty($datas['adminReg']) || empty($datas['platformId']) || empty($datas['adminUserId'])) {
					$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
					return json_encode($data);
				}
				

				$filename['content'] = getStr($datas['content'], 'string');
				$filename['status'] = $datas['status']?getStr($datas['status'], 'int'):1;
				$filename['platformId'] = getStr($datas['platformId'], 'int');
				$filename['adminUserId'] = getStr($datas['adminUserId'], 'int');
				$filename['adminReg'] = getStr($datas['adminReg'], 'string');
				try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
				$api = new AttAdminBulletinServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new bulletinDTO();
				$engprojectDo->filename = $filename;
				$result = $api->addBulletin($engprojectDo);

				if($result->code){
					$data = ['msg'=>'成功','code'=>'200','data'=>[]];
				} else {
					$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);

	}
	
	//修改公告
	public function editBulletin(){
				$datas = $this->request->post();
				 //测试用
				if (empty($datas['id']) || empty($datas['content']) || empty($datas['adminReg']) ) {
					$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
					return json_encode($data);
				}
				
				$id = getStr($datas['id'], 'int');
				$filename['content'] = getStr($datas['content'], 'string');
				$filename['status'] = $datas['status']?getStr($datas['status'], 'int'):1;
				$filename['adminReg'] = getStr($datas['adminReg'], 'string'); //题库

				try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
				$api = new AttAdminBulletinServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new bulletinDTO();
				$engprojectDo->filename = $filename;
				$engprojectDo->id = $id;
				$result = $api->editBulletin($engprojectDo);

				if($result->code){
					$data = ['msg'=>'成功','code'=>'200','data'=>[]];
				} else {
					$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);

	}

    //操作公告
    public function operationBulletin(){

        $datas = $this->request->post();
        //测试用
        if (empty($datas['content']) || empty($datas['adminReg']) ) {
            $data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
            return json_encode($data);
        }

        $id = getStr($datas['id'], 'int');
        $filename['content'] = getStr($datas['content'], 'string');
        $filename['status'] = $datas['status']?getStr($datas['status'], 'int'):1;
        $do = $datas['do']?getStr($datas['do'], 'int'):1;//1修改 2添加
        $filename['adminReg'] = getStr($datas['adminReg'], 'string'); //题库
        $filename['platformId'] = getStr($datas['platformId'], 'int');
        $filename['adminUserId'] = getStr($datas['adminUserId'], 'int');
        try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $api = new AttAdminBulletinServiceClient(NULL);
            \ApiClient::build($api);
            $engprojectDo = new bulletinDTO();
            $engprojectDo->filename = $filename;
            if ($do==1){
                $engprojectDo->id = $id;
                $result = $api->editBulletin($engprojectDo);
            }else{
                $result = $api->addBulletin($engprojectDo);
            }


            if($result->code){
                $data = ['msg'=>'成功','code'=>'200','data'=>[]];
            } else {
                $data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
            }
        } catch (Exception $ex) {
            $data = ['msg'=>'失败','code'=>'400','data'=>[]];
        }
        return json_encode($data);

    }

	//查看公告
	public function lookOverBulletin(){

		$id = $this->request->param('id') ? $this->request->param('id/d') : 0;
        $memberId = $this->request->param('userId') ? $this->request->param('userId/d') : 0;
	/*	$adminReg = 'j4timvso_54lkl9x3flob595f327ba4cb3gdl_test';
		$id = '2';*/
        $token = $this->request->param('token') ? getStr($this->request->param('token')) : '';
        if(!$memberId || $token==''){
            $msg =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
            return json_encode($msg);
        }
        $token = md5("2".$token);
        if(!checkToken($token,$memberId)){
            $msg['code'] = 501;
            $msg['msg'] ='用户登录失效，请重新登录'; return json_encode($msg);
        }
		if(!$id){
			
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		
		}

				try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
				$api = new AttAdminBulletinServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new BulletinDTO();
				$engprojectDo->id = $id;
				$result = $api->lookOver($engprojectDo);

				if($result->code){
					$data = ['msg'=>'成功','code'=>'200','data'=>$result->data[0]];
		
				} else {
					$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);
	}
	
	
	public function deleteBulletin()
    {
		$adminReg = $this->request->param('adminReg') ? $this->request->param('adminReg') : '';
		$ids = $this->request->param('ids') ? $this->request->param('ids') : 0;//ID集合
		$del = $this->request->param('del');//逻辑删除  物理删除 恢复
		if($adminReg=='' || !$ids){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}
		if($del == 'deletes'){//逻辑删除
			$filename['isdelete'] = 2;
		} else if($del == 'removes'){//物理删除
			$filename['isdelete'] = 'removes';
		} else if($del == 'reduction'){//恢复
			$filename['isdelete'] = 1                ;
		} else {
			$data = ['msg'=>'参数错误','code'=>'500'];
			return json_encode($data);
		}
	try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
           	$api = new AttAdminBulletinServiceClient(NULL);
			\ApiClient::build($api);
			$engprojectDo = new bulletinDTO();
            $engprojectDo->filename = $filename;
			$engprojectDo->ids = $ids;
			$engprojectDo->adminReg = $adminReg;
            $result = $api->deleteBulletin($engprojectDo);

			if ($result->code) {
               $data = ['msg'=>$result->message,'code'=>'200','data'=>[]];
            } else {
               $data = ['msg'=>'操作失败','code'=>'200','data'=>[]];
            }
		} catch (Exception $ex) {
            $data = ['msg'=>'发生异常，操作失败','code'=>'400','data'=>[]];
        }
		 return json_encode($data);
    }
    
	public function bulletinStatus()
    {
		$adminReg = $this->request->param('adminReg') ? $this->request->param('adminReg') : '';
		$id = $this->request->param('id') ? $this->request->param('id') : 0;
        $platformId = $this->request->param('platformId') ? $this->request->param('platformId') : 0;
		if($platformId=='a' || !$id){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}
		$filename['status'] = $this->request->param('status')=='enable' ? 1 : 0;//enable开启  disabled禁用
        try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
           	$api = new AttAdminBulletinServiceClient(NULL);
			\ApiClient::build($api);
			$engprojectDo = new bulletinDTO();
            $engprojectDo->filename = $filename;
			$engprojectDo->id = $id;
			$engprojectDo->adminReg = $adminReg;
            $result = $api->stopBulletin($engprojectDo);
			if ($result->code) {
               $data = ['msg'=>'操作成功','code'=>'200','data'=>[]];
            } else {
               $data = ['msg'=>'操作失败','code'=>'200','data'=>[]];
            }
		} catch (Exception $ex) {
            $data = ['msg'=>'发生异常，操作失败','code'=>'400','data'=>[]];
        }
		 return json_encode($data);
    }
	
	//app最近的公告
	public function appList(){

		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId'), 'int') : 0;
        $memberId = $this->request->param('userId') ? $this->request->param('userId/d') : 0;
        $num = $this->request->param('num') ? $this->request->param('num/d') : 0;
        $offset = $this->request->param('offset') ? $this->request->param('offset/d') : 0;
        /*	$adminReg = 'j4timvso_54lkl9x3flob595f327ba4cb3gdl_test';
            $id = '2';*/
        $token = $this->request->param('token') ? getStr($this->request->param('token')) : '';
        if(!$memberId || $token==''){
            $msg =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
            return json_encode($msg);
        }
//        $token = md5("2".$token);
        if(!checkToken($token,$memberId)){
            $msg['code'] = 501;
            $msg['msg'] ='用户登录失效，请重新登录'; return json_encode($msg);
        }
		if(!$platformId){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new AttAdminBulletinServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new bulletinDTO();
				$engprojectDo->platformId = $platformId;
				$engprojectDo->data = ['offset'=>$offset,'num'=>$num];

				$result = $api->appBulletinList($engprojectDo);

				if($result->code){
					$data = ['msg'=>'成功','code'=>'200','data'=>['list'=>$result->data?$result->data:[],'count'=>$result->counts?$result->counts:0]];
				} else {
					$data = ['msg'=>'失败','code'=>'400','data'=>['list'=>[],'count'=>0]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'失败','code'=>'400','data'=>['list'=>[],'count'=>0]];
			}

			return json_encode($data);

	}
	

}
